import { get, post, patch, del } from './api-caller';

const getPostCollections = () => get<PostCollection[]>({ path: '/api/post-collections' });

const getPostCollectionById = (id: number) => get<PostCollection>({ path: `/api/post-collections/${id}` });

type SavePostCollectionPayload = {
  title: string;
};

const createPostCollection = (payload: SavePostCollectionPayload) =>
  post<PostCollection>({ path: '/api/post-collections', body: payload });

const updatePostCollection = (id: number, payload: SavePostCollectionPayload) =>
  patch<PostCollection>({ path: `/api/post-collections/${id}`, body: payload });

const deletePostCollection = (id: number) => del<void>({ path: `/api/post-collections/${id}` });

type CreatePostPayload = {
  title: string;
  type: PostType;
  content: string;
  collectionId: number;
  tagIds: number[];
  dedupKey: string;
};

const createPost = (payload: CreatePostPayload) => post<Post>({ path: '/api/posts', body: payload });

type UpdatePostPayload = {
  title: string;
  type: PostType;
  content: string;
};

const updatePost = (id: number, payload: UpdatePostPayload) => patch<Post>({ path: `/api/posts/${id}`, body: payload });

const getPostById = (id: number) => get<Post>({ path: `/api/posts/${id}` });

type GetPostsParams = {
  collectionId: number;
};

const getPosts = (params: GetPostsParams) => get<Post[]>({ path: `/api/posts`, searchParams: params });

const deletePost = (id: string) => del<void>({ path: `/api/posts/${id}` });

const postApis = {
  getPostCollections,
  getPostCollectionById,
  createPostCollection,
  updatePostCollection,
  deletePostCollection,
  createPost,
  updatePost,
  getPostById,
  getPosts,
  deletePost,
};

export default postApis;
